const express = require('express');
const session = require('express-session');
const app = express();
const path = require('path');
const loginRouter = require('./router/loginRouter');

// 导入checkLogin中间件，检查是否登录
const {checkLogin} = require('./middware/checkLogin');
const globalParams = require('./middware/globalParams');

// console.log('users: ', users, typeof users, users instanceof Array);// 确定引入json文件的数据类型

// 支持post数据
app.use(express.urlencoded({ extended: false }));
// 支持session
app.use(session({
    secret: 'atguigu',
    saveUninitialized: false,
    resave: false,
    cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 15
    }
}));
// 支持ejs
app.set('views', path.join(__dirname, 'views'));
app.engine('.html', require('ejs').__express);
app.set('view engine', 'html');

// 设置静态资源
app.use(express.static(path.resolve(__dirname, './public')));

app.use(globalParams);

app.use(loginRouter);

// 首页
app.get('/index', (req, res) => {
    res.render('index');
})
// 用户中心页
app.get('/ucenter',checkLogin, (req, res) => {
    console.log(req.session.user);
    res.render('ucenter');
})

app.get('/order',checkLogin, (req,res)=>{
    res.render('order');
})


app.listen(8080, () => {
    console.log('server run at 8080');
})